<?php
class Users extends CI_Controller{
	function __construct(){
		parent::__construct();
		if (!$this->session->userdata('name')){
			header("Location: /");
		}
		$this->load->model('users_model');
	}
	function index(){
		$data['sidebar'] = array('system','users');
		$data['query'] = $this->users_model->view();
		load_view('users',$data);
	}
	
	function new_user(){
		$data['sidebar'] = array('system','users');
		$data['query'] = '';
		load_view('users_file',$data);
	}
	
	function modify_user($id){
		$obj = new Users_model($id);		
		$data['sidebar'] = array('system','users');
		$data['query'] = array(
			'id' => $id,
			'name' => $obj->name,
			'pass' => $obj->pass,
			'permission' => unserialize($obj->permission),
		);
		load_view('users_file',$data);
	}
	
	function execute(){
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		$pass = $this->input->post('pass');
		$permission = $this->input->post('permission');
		
		$obj = new Users_model($id);
		$obj->name = trim($name);
		$obj->pass = trim($pass);
		$obj->permission = serialize($permission);
		if($id){
			$obj->update();
		}else{
			$obj->save();
		}
		header("Location: /users");
	}
	
	function del_user($id){
		$obj = new Users_model($id);
		$obj->delete();
		header("Location: /users");
	}
}